package com.cn.ant.modules.wechat.service.function.impl;


import com.cn.ant.common.utils.SpringContextHolder;
import com.cn.ant.modules.wechat.entity.message.resp.Article;
import com.cn.ant.modules.wechat.entity.message.resp.NewsMessageResp;
import com.cn.ant.modules.wechat.entity.message.resp.TextMessageResp;
import com.cn.ant.modules.wechat.service.WeixinAccountServiceImpl;
import com.cn.ant.modules.wechat.service.function.KeyServiceI;
import com.cn.ant.modules.wechat.utils.MessageUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

public class SpecificLinkService implements KeyServiceI {


	@Override
	public String getKey() {
		return "XXRS#";
	}

	@Override
	public String excute(String content, TextMessageResp defaultMessage, HttpServletRequest request) {
		// 获取链接的唯一标识
		String cardNo = content.trim().substring(content.indexOf("#")+1);
		//查询用户是否存在
        WeixinAccountServiceImpl weixinAccountService = (WeixinAccountServiceImpl) SpringContextHolder.getApplicationContext().getBean("weixinAccountService");
		String accountid = weixinAccountService.findByToUsername(defaultMessage.getFromUserName()).getId();
		ResourceBundle bundler = ResourceBundle.getBundle("application");
		List<Article> articleList = new ArrayList<Article>();
		Article article = new Article();
		article.setTitle("鑫享人生,金喜无限");
		article.setDescription("鑫享人生金喜无限,免费送电影");
		article.setPicUrl(bundler.getString("domain") + "/static/images/wechat/tk/mini-movie.png");
		article.setUrl(bundler.getString("domain") + "/app/tk/movie.do?enter&accountid=" + accountid + "&cardNo=" + cardNo + "&openId=" + defaultMessage.getToUserName());
		articleList.add(article);
		NewsMessageResp newsMessage = new NewsMessageResp();
		newsMessage.setToUserName(defaultMessage.getToUserName());
		newsMessage.setFromUserName(defaultMessage.getFromUserName());
		newsMessage.setCreateTime(new Date().getTime());
		newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
		newsMessage.setArticleCount(articleList.size());
		newsMessage.setArticles(articleList);
		return MessageUtil.newsMessageToXml(newsMessage);
	}

}
